<?php
define('IN_TG','basic');
define('IN_JS','jquery-1.12.0.min');
define('SCRIPT','upimg');
require("includes/common.inc.php");
require('includes/title.inc.php');
if(isset($_GET['action']))
{
	if($_GET['action']=='up')
	{
		if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
			$dir_path=$_POST['dir'];
			//后缀
			$ext=pathinfo($_FILES['userfile']['name'],PATHINFO_EXTENSION);
			$file_name=time().mt_rand().'.'.$ext;
			$allow=array('gif','jpg','txt','doc','png');
			if(in_array($ext,$allow))
			{
				$old_path=$_FILES['userfile']['tmp_name'];
				$new_path=$dir_path.'/'.$file_name;
				#移动文件,进行统一管理
				if(move_uploaded_file($old_path,$new_path))
				{
					echo <<<eof
            <script>
                $("#url",window.opener.document).val("{$new_path}");
                window.close();
            </script>
eof;
				}
				else
				{
					_alert_back("上传失败");
				}
			}
			else
			{
				_alert_back("文件不支持上传");
			}
		}
		else
		{
			_alert_back("文件上传异常");
		}
	}
}

?>
<body>

<div id="upimg" style="padding:20px;">
	<form enctype="multipart/form-data" action="upimg.php?action=up" method="post">
		<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
		<input type="hidden" name="dir" value="<?php echo $_GET['dir']?>" />
		选择图片: <input type="file" name="userfile" />
		<input type="submit" value="上传" />
	</form>
</div>
</body>
</html>